package com.hzdl.car.controller;

import com.hzdl.car.bean.*;
import com.hzdl.car.service.CarReturnService;
import com.hzdl.car.util.AJAXResult;
import com.hzdl.car.vo.CarReturnVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.Random;

@Controller
@RequestMapping("/carReturn")
public class CarReturnController {

    @Autowired
    private CarReturnService crs;

    @RequestMapping("/carReturnInfoInit")
    public String carReturnInfoInit(){
        return "/system/CarReturn";
    }
    @RequestMapping("/checkRentId")
    @ResponseBody
    public AJAXResult checkRentId(String rentid){
        CarRentInfo rentInfo=crs.findCheck(rentid);
        return new AJAXResult(rentInfo);
    }
    @RequestMapping("/relativeCarInfoShow")
    @ResponseBody
    public AJAXResult relativeCarInfoShow(String rentid, HttpSession session){
        Map<String,Object> map = new HashMap<>();
        CarRentInfo rentInfo= crs.findCheck(rentid);
        map.put("rentInfo",rentInfo);
        CarReturn check = crs.findCheck1(rentid);
        map.put("check",check);
        Car carInfo = crs.findCheck2(rentInfo.getCarnumber());
        map.put("carInfo",carInfo);
        Customer customer = crs.findCheck3(rentInfo.getIdentity());
        map.put("customer",customer);
        SimpleDateFormat sdf1 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String createtime=sdf1.format(new Date());
        map.put("createtime",createtime);
        SimpleDateFormat sdf2 = new SimpleDateFormat("yyyyMMdd");
        String l="JC_";
        l+=sdf2.format(new Date());
        Random r = new Random();
        for(int i =0;i<6;i++){
            l+=r.nextInt(10);
        }
        l+="_";
        for(int j =0;j<4;j++){
            l+=r.nextInt(10);
        }
        l+="_";
        for(int k =0;k<5;k++){
            l+=r.nextInt(10);
        }
        User user=(User) session.getAttribute("user");
        map.put("returnNumber",l);
        map.put("user",user);

        return new AJAXResult(0,"",map);

    }
    @RequestMapping("/returnCar")
    @ResponseBody
    public AJAXResult returnCar(CarReturnVo carReturnVo,String carnumber){
        int row1=crs.insertCheck(carReturnVo);
        row1+=crs.updateCarInfo(carnumber);
        row1+=crs.updateCarRentInfo(carReturnVo.getRentid());
        if(row1==0){
            return new AJAXResult(0,"还车失败");
        }
        return new AJAXResult(1,"还车成功");
    }


}
